/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Typestatus;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Identificationelement;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Curatorialelement;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class TypestatusJpaController {

    public TypestatusJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Typestatus save(Typestatus typestatus) {
        Typestatus aux = findTypestatus(typestatus.getTypestatus());
        if(aux==null)
            return create(typestatus);
        else
            return aux;
    }

    public Typestatus create(Typestatus typestatus) {
        Typestatus r = null;
        if (typestatus.getIdentificationelementList() == null) {
            typestatus.setIdentificationelementList(new ArrayList<Identificationelement>());
        }
        if (typestatus.getCuratorialelementList() == null) {
            typestatus.setCuratorialelementList(new ArrayList<Curatorialelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Identificationelement> attachedIdentificationelementList = new ArrayList<Identificationelement>();
            for (Identificationelement identificationelementListIdentificationelementToAttach : typestatus.getIdentificationelementList()) {
                identificationelementListIdentificationelementToAttach = em.getReference(identificationelementListIdentificationelementToAttach.getClass(), identificationelementListIdentificationelementToAttach.getIdidentificationelement());
                attachedIdentificationelementList.add(identificationelementListIdentificationelementToAttach);
            }
            typestatus.setIdentificationelementList(attachedIdentificationelementList);
            List<Curatorialelement> attachedCuratorialelementList = new ArrayList<Curatorialelement>();
            for (Curatorialelement curatorialelementListCuratorialelementToAttach : typestatus.getCuratorialelementList()) {
                curatorialelementListCuratorialelementToAttach = em.getReference(curatorialelementListCuratorialelementToAttach.getClass(), curatorialelementListCuratorialelementToAttach.getIdcuratorialelement());
                attachedCuratorialelementList.add(curatorialelementListCuratorialelementToAttach);
            }
            typestatus.setCuratorialelementList(attachedCuratorialelementList);
            r = em.merge(typestatus);
            for (Identificationelement identificationelementListIdentificationelement : typestatus.getIdentificationelementList()) {
                identificationelementListIdentificationelement.getTypestatusList().add(typestatus);
                identificationelementListIdentificationelement = em.merge(identificationelementListIdentificationelement);
            }
            for (Curatorialelement curatorialelementListCuratorialelement : typestatus.getCuratorialelementList()) {
                curatorialelementListCuratorialelement.getTypestatusList().add(typestatus);
                curatorialelementListCuratorialelement = em.merge(curatorialelementListCuratorialelement);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Typestatus typestatus) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typestatus persistentTypestatus = em.find(Typestatus.class, typestatus.getIdtypestatus());
            List<Identificationelement> identificationelementListOld = persistentTypestatus.getIdentificationelementList();
            List<Identificationelement> identificationelementListNew = typestatus.getIdentificationelementList();
            List<Curatorialelement> curatorialelementListOld = persistentTypestatus.getCuratorialelementList();
            List<Curatorialelement> curatorialelementListNew = typestatus.getCuratorialelementList();
            List<Identificationelement> attachedIdentificationelementListNew = new ArrayList<Identificationelement>();
            for (Identificationelement identificationelementListNewIdentificationelementToAttach : identificationelementListNew) {
                identificationelementListNewIdentificationelementToAttach = em.getReference(identificationelementListNewIdentificationelementToAttach.getClass(), identificationelementListNewIdentificationelementToAttach.getIdidentificationelement());
                attachedIdentificationelementListNew.add(identificationelementListNewIdentificationelementToAttach);
            }
            identificationelementListNew = attachedIdentificationelementListNew;
            typestatus.setIdentificationelementList(identificationelementListNew);
            List<Curatorialelement> attachedCuratorialelementListNew = new ArrayList<Curatorialelement>();
            for (Curatorialelement curatorialelementListNewCuratorialelementToAttach : curatorialelementListNew) {
                curatorialelementListNewCuratorialelementToAttach = em.getReference(curatorialelementListNewCuratorialelementToAttach.getClass(), curatorialelementListNewCuratorialelementToAttach.getIdcuratorialelement());
                attachedCuratorialelementListNew.add(curatorialelementListNewCuratorialelementToAttach);
            }
            curatorialelementListNew = attachedCuratorialelementListNew;
            typestatus.setCuratorialelementList(curatorialelementListNew);
            typestatus = em.merge(typestatus);
            for (Identificationelement identificationelementListOldIdentificationelement : identificationelementListOld) {
                if (!identificationelementListNew.contains(identificationelementListOldIdentificationelement)) {
                    identificationelementListOldIdentificationelement.getTypestatusList().remove(typestatus);
                    identificationelementListOldIdentificationelement = em.merge(identificationelementListOldIdentificationelement);
                }
            }
            for (Identificationelement identificationelementListNewIdentificationelement : identificationelementListNew) {
                if (!identificationelementListOld.contains(identificationelementListNewIdentificationelement)) {
                    identificationelementListNewIdentificationelement.getTypestatusList().add(typestatus);
                    identificationelementListNewIdentificationelement = em.merge(identificationelementListNewIdentificationelement);
                }
            }
            for (Curatorialelement curatorialelementListOldCuratorialelement : curatorialelementListOld) {
                if (!curatorialelementListNew.contains(curatorialelementListOldCuratorialelement)) {
                    curatorialelementListOldCuratorialelement.getTypestatusList().remove(typestatus);
                    curatorialelementListOldCuratorialelement = em.merge(curatorialelementListOldCuratorialelement);
                }
            }
            for (Curatorialelement curatorialelementListNewCuratorialelement : curatorialelementListNew) {
                if (!curatorialelementListOld.contains(curatorialelementListNewCuratorialelement)) {
                    curatorialelementListNewCuratorialelement.getTypestatusList().add(typestatus);
                    curatorialelementListNewCuratorialelement = em.merge(curatorialelementListNewCuratorialelement);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = typestatus.getIdtypestatus();
                if (findTypestatus(id) == null) {
                    throw new NonexistentEntityException("The typestatus with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typestatus typestatus;
            try {
                typestatus = em.getReference(Typestatus.class, id);
                typestatus.getIdtypestatus();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The typestatus with id " + id + " no longer exists.", enfe);
            }
            List<Identificationelement> identificationelementList = typestatus.getIdentificationelementList();
            for (Identificationelement identificationelementListIdentificationelement : identificationelementList) {
                identificationelementListIdentificationelement.getTypestatusList().remove(typestatus);
                identificationelementListIdentificationelement = em.merge(identificationelementListIdentificationelement);
            }
            List<Curatorialelement> curatorialelementList = typestatus.getCuratorialelementList();
            for (Curatorialelement curatorialelementListCuratorialelement : curatorialelementList) {
                curatorialelementListCuratorialelement.getTypestatusList().remove(typestatus);
                curatorialelementListCuratorialelement = em.merge(curatorialelementListCuratorialelement);
            }
            em.remove(typestatus);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Typestatus> findTypestatusEntities() {
        return findTypestatusEntities(true, -1, -1);
    }

    public List<Typestatus> findTypestatusEntities(int maxResults, int firstResult) {
        return findTypestatusEntities(false, maxResults, firstResult);
    }

    private List<Typestatus> findTypestatusEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Typestatus.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Typestatus findTypestatus(String q) {
        Query query= getEntityManager().createNamedQuery("Typestatus.findByTypestatus");
        query.setParameter("typestatus",q);

        return query.getResultList().isEmpty()?null:(Typestatus)query.getResultList().get(0);
    }

    public Typestatus findTypestatus(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Typestatus.class, id);
        } finally {
            em.close();
        }
    }

    public int getTypestatusCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Typestatus> rt = cq.from(Typestatus.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
